<?php
namespace app\admin\controller;

use think\Db;
use think\Controller;

class School extends Controller
{
    public function index()
    {
        $school = Db::name("school")->select();
        $this->assign("school", $school);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }
    public function doaddsname($schoolname)
    {
        // $_GET
        $data = ['schoolName' => $schoolname];
        $res = Db::table('school')->insert($data);
        if($res){
            $this->redirect('index');
        }else{
            $this -> error('add failure :(','index');
        }
    }

    public function edit($sid)
    {
        $school = Db::name('school')->where('schoolId',$sid)->find(); 
        // dump($role);
        $this->assign('school', $school);
        return $this->fetch();
    }

    public function del($sid)
    {
        if (Db::name('school')->delete($sid)) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('删除失败', 'index');
        }
    }

    public function update($sid, $content)
    {
        // var_dump($content);
        $res = Db::name('school')->where('schoolId', $sid)->update(['schoolName' => $content]);
        if ($res) {
            $this->success("修改成功", 'index');
        } else {
            $this->error("修改失败", 'index');
        }
    }

    public function search()
    {
        $key = input('key');
        $res = Db::name('school')->whereLike('schoolName', '%' . $key . '%')->select();
        echo json_encode($res);
    }

}
 